#include "AoiReq.h"

using namespace aoi;



QJsonDocument AoiReq::toQJsonDocument()
{
	QJsonDocument jsonDoc;
	QJsonObject jsonRoot;
	jsonRoot.insert("ProjectName", projectName);
	jsonRoot.insert("TaskName", taskName);
	jsonRoot.insert("TaskType", taskType);
	jsonRoot.insert("MachineId", machineId);
	jsonRoot.insert("ProductCode", productCode);
	jsonRoot.insert("TableName", tableName);
	jsonRoot.insert("CameraID", cameraId);
	jsonRoot.insert("CodeType", "JPG");

	//Files
	if (!reqFiles.isEmpty())
	{
		QJsonArray filesArr;
		QListIterator<ReqFile> iter(reqFiles);
		while (iter.hasNext())
		{
			ReqFile reqFile = iter.next();
			QJsonObject fileObj;
			fileObj.insert("FileName", reqFile.fileName);
			fileObj.insert("ImageData", reqFile.imageData);
			filesArr.append(fileObj);
		}
		jsonRoot.insert("Files", filesArr);
	}

	// MatchParams
	QJsonObject matchParamObj;
	QJsonObject globalThreshObj;
	globalThreshObj.insert("conf_thresh", matchParams.globalThresh.confThresh);
	globalThreshObj.insert("overlap_metric", matchParams.globalThresh.overlapMetric);
	globalThreshObj.insert("overlap_thresh", matchParams.globalThresh.overlapThresh);
	matchParamObj.insert("global_thresh", globalThreshObj);
	QJsonArray labelSpecThreshArr;
	QListIterator<LabelSpecThresh> iter(matchParams.labelSpecThreshList);
	while (iter.hasNext())
	{
		QJsonObject oneLabelSpecThreshObj;
		LabelSpecThresh oneLabelSpecThresh = iter.next();
		oneLabelSpecThreshObj.insert("label", oneLabelSpecThresh.label);
		oneLabelSpecThreshObj.insert("conf_thresh", oneLabelSpecThresh.confThresh);
		oneLabelSpecThreshObj.insert("overlap_metric", oneLabelSpecThresh.overlapMetric);
		oneLabelSpecThreshObj.insert("overlap_thresh", oneLabelSpecThresh.overlapThresh);
		labelSpecThreshArr.append(oneLabelSpecThreshObj);
	}
	matchParamObj.insert("label_spec_thresh", labelSpecThreshArr);
	jsonRoot.insert("MatchParams", matchParamObj);

	//ExtraParams
	QJsonObject extraParamObj;
	{
		extraParamObj.insert("extra_name", extraParams.extra_name);
		extraParamObj.insert("extra_name2", extraParams.extra_name2);
	}
	jsonRoot.insert("ExtraParams", extraParamObj);

	//ROIs
	if (!rois.isEmpty())
	{
		QJsonArray roiArr;
		QListIterator<Roi> iter(rois);
		while (iter.hasNext())
		{
			Roi oneRoi = iter.next();
			QJsonObject oneRoiObj;
			oneRoiObj.insert("Type", oneRoi.type);
			oneRoiObj.insert("Height", oneRoi.height);
			oneRoiObj.insert("Width", oneRoi.width);
			oneRoiObj.insert("X", oneRoi.X);
			oneRoiObj.insert("Y", oneRoi.Y);
			roiArr.append(oneRoiObj);
		}
		jsonRoot.insert("ROIs", roiArr);
	}

	//RegisterMatrix
	if (!registerMatrix.isEmpty())
	{
		QJsonArray matrixArr;
		for (int i = 0; i < registerMatrix.size(); i++)
		{
			QJsonArray row;
			for (int j = 0; j < registerMatrix[0].size(); j++)
			{
				row.append(registerMatrix[i][j]);
			}
			matrixArr.append(row);
		}
		jsonRoot.insert("RegisterMatrix", matrixArr);
	}

	//Patterns
	if (!patterns.isEmpty())
	{
		QJsonArray patternArr;
		QListIterator<Pattern> iter(patterns);
		while (iter.hasNext())
		{
			Pattern onePattern = iter.next();
			QJsonObject onePatternObj;
			onePatternObj.insert("Label", onePattern.label);
			onePatternObj.insert("Height", onePattern.height);
			onePatternObj.insert("Width", onePattern.width);
			onePatternObj.insert("X", onePattern.X);
			onePatternObj.insert("Y", onePattern.Y);
			onePatternObj.insert("conf_thresh", onePattern.confThresh);
			onePatternObj.insert("detail_label", onePattern.detailLabel);
			onePatternObj.insert("num_match", onePattern.numMatch);
			onePatternObj.insert("overlap_metric", onePattern.overlapMetric);
			onePatternObj.insert("overlap_thresh", onePattern.overlapThresh);
			onePatternObj.insert("reserved1", onePattern.reserved1);
			onePatternObj.insert("reserved2", onePattern.reserved2);
			onePatternObj.insert("reserved3", onePattern.reserved3);
			onePatternObj.insert("reserved4", onePattern.reserved4);
			onePatternObj.insert("reserved5", onePattern.reserved5);
			onePatternObj.insert("reserved6", onePattern.reserved6);
			patternArr.append(onePatternObj);
		}
		jsonRoot.insert("Patterns", patternArr);
	}

	jsonDoc.setObject(jsonRoot);
	return jsonDoc;
}

QByteArray AoiReq::toJsonQByteArray()
{
	return toQJsonDocument().toJson(QJsonDocument::Compact);
}

QString AoiReq::toJsonQString()
{
	QString strJson(toJsonQByteArray());
	return strJson;
}

LabelSpecThresh::LabelSpecThresh()
{
}

LabelSpecThresh::LabelSpecThresh(QString label, float oneConfThresh, QString overlapMetric, float overlapThresh)
{
	this->label = label;
	this->confThresh = oneConfThresh;
	this->overlapMetric = overlapMetric;
	this->overlapThresh = overlapThresh;
}

Pattern::Pattern()
{
}

Pattern::Pattern(QString label, int width, int height, int X, int Y, float confThresh, QString detailLabel, int numMatch, QString overlapMetric, int overlapThresh)
{
	this->label = label;
	this->width = width;
	this->height = height;
	this->X = X;
	this->Y = Y;
	this->confThresh = confThresh;
	this->detailLabel = detailLabel;
	this->numMatch = numMatch;
	this->overlapMetric = overlapMetric;
	this->overlapThresh = overlapThresh;
}

Roi::Roi()
{
}

Roi::Roi(int x, int y, int width, int height,  QString type)
{
	this->X = x;
	this->Y = y;
	this->width = width;
	this->height = height;
	this->type = type;
}

ReqFile::ReqFile()
{
}

ReqFile::ReqFile(QString fileName, QString imageData)
{
	this->fileName = fileName;
	this->imageData = imageData;
}

GlobalThresh::GlobalThresh()
{
}

GlobalThresh::GlobalThresh(float confThresh, QString overlapMetric, float overlapThresh)
{
	this->confThresh = confThresh;
	this->overlapMetric = overlapMetric;
	this->overlapThresh = overlapThresh;
}